//
//  ViewController.m
//  UI14_HomeWork_豆瓣
//
//  Created by Bodi on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "MovieModel.h"
#import "BodiCollectionViewCell.h"
#import "MovieViewController.h"
#import "MovieDetailsViewController.h"

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain)NSMutableArray *modelArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.modelArray = [NSMutableArray array];
    [self handleData];
    [self creatwCollectionView];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_list"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
}
- (void)rightAction:(UIBarButtonItem *)item{
    MovieViewController *vc = [[MovieViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)handleData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"movielist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    /* 解析json格式 */
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    NSArray *resultArray = [dictionary objectForKey:@"result"];
    for (NSDictionary *dic in resultArray) {
        MovieModel *model = [[MovieModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.modelArray addObject:model];
        [model release];
    }
    
}

- (void)creatwCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(100, 150);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 50;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 50, 50, 20);
    
    
    UICollectionView *rainCollectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    [self.view addSubview:rainCollectionView];
    [flowLayout release];
    [rainCollectionView release];
    rainCollectionView.backgroundColor = [UIColor whiteColor];
    rainCollectionView.delegate = self;
    rainCollectionView.dataSource = self;
    [rainCollectionView registerClass:[BodiCollectionViewCell class] forCellWithReuseIdentifier:@"sell"];
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.modelArray.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BodiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"sell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor redColor];
    MovieModel *model = [self.modelArray objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MovieDetailsViewController *modc = [[MovieDetailsViewController alloc]init];
    modc.movieModel = [self.modelArray objectAtIndex:indexPath.row];
//    [self.navigationController pushViewController:modc animated:YES];
    [self.navigationController popToRootViewControllerAnimated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
